package com.mjf.cjt.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mjf.cjt.domain.TFReceiptOrder;
import com.mjf.cjt.domain.TFWriteOffDetails;
import com.mjf.cjt.mapper.TFReceiptOrderMapper;
import com.mjf.cjt.service.AsyncTMService;
import com.mjf.cjt.service.TFReceiptOrderService;
import com.mjf.cjt.service.TFWriteOffDetailsService;
import com.mjf.cjt.vo.ArapReceivePaymentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * desc
 * </p>
 *
 * @author chenshoumeng
 * @date 2025/5/22 15:49
 */
@Service
public class TFReceiptOrderServiceImpl extends ServiceImpl<TFReceiptOrderMapper, TFReceiptOrder> implements TFReceiptOrderService {

    @Autowired
    private AsyncTMService asyncTMService;
//    @Autowired
//    private TFArapMultiSettleDetailsService tfArapMultiSettleDetailsService;
    @Autowired
    private TFWriteOffDetailsService tfWriteOffDetailsService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void asyncSKD(String start, String end) {
        List<ArapReceivePaymentVO> arapReceivePaymentVOS = asyncTMService.asyncSKD(start, end);

        // 收款单分组
        Map<Long, List<ArapReceivePaymentVO>> groupList =
            arapReceivePaymentVOS.stream().collect(Collectors.groupingBy(ArapReceivePaymentVO::getId));
        List<TFReceiptOrder> tfReceiptOrders = new ArrayList<>();
//        List<TFArapMultiSettleDetails> arapMultiSettleDetails = new ArrayList<>();
        List<TFWriteOffDetails> writeOffDetails = new ArrayList<>();
        for (Map.Entry<Long, List<ArapReceivePaymentVO>> entry : groupList.entrySet()) {
            TFReceiptOrder tfReceiptOrder = new TFReceiptOrder();
            tfReceiptOrder.setId(entry.getKey());
            tfReceiptOrder.setOrderCode(entry.getValue().get(0).getCode());
            if (StrUtil.isNotBlank(entry.getValue().get(0).getVoucherdate())) {
                String dt = entry.getValue().get(0).getVoucherdate().replace("/", "-");
                tfReceiptOrder.setOrderDate(LocalDate.parse(dt));
            }
            tfReceiptOrder.setCustomerCode(entry.getValue().get(0).getPartner_code());
            tfReceiptOrder.setCustomerName(entry.getValue().get(0).getPartner_name());
            tfReceiptOrders.add(tfReceiptOrder);
//            // 按结算单分组
//            Map<Long, List<ArapReceivePaymentVO>> jsdMap =
//                entry.getValue().stream().collect(Collectors.groupingBy(ArapReceivePaymentVO::getArapmultisettledetails_id));
//            for (Map.Entry<Long, List<ArapReceivePaymentVO>> jsdEntry : jsdMap.entrySet()) {
//                TFArapMultiSettleDetails tfArapMultiSettleDetails = new TFArapMultiSettleDetails();
//                tfArapMultiSettleDetails.setId(jsdEntry.getKey());
//                tfArapMultiSettleDetails.setReceiptId(tfReceiptOrder.getId());
//                tfArapMultiSettleDetails.setReceiptCode(tfReceiptOrder.getOrderCode());
//                tfArapMultiSettleDetails.setSettleStyleCode(jsdEntry.getValue().get(0).getArapmultisettledetails_settlestyle_code());
//                tfArapMultiSettleDetails.setSettleStyleName(jsdEntry.getValue().get(0).getArapmultisettledetails_settlestyle_name());
//                tfArapMultiSettleDetails.setOrigAmount(jsdEntry.getValue().get(0).getArapmultisettledetails_origamount());
//                arapMultiSettleDetails.add(tfArapMultiSettleDetails);
//            }
            // 按核销明细ID分组
            Map<Long, List<ArapReceivePaymentVO>> hmdMap =
                entry.getValue().stream().filter(hmd -> ObjectUtil.isNotNull(hmd.getDetails_id())).collect(Collectors.groupingBy(ArapReceivePaymentVO::getDetails_id));
                for (Map.Entry<Long, List<ArapReceivePaymentVO>> hmdEntry : hmdMap.entrySet()) {
                    TFWriteOffDetails tfWriteOffDetails = new TFWriteOffDetails();
                    tfWriteOffDetails.setId(hmdEntry.getKey());
                    tfWriteOffDetails.setOrderId(tfReceiptOrder.getId());
                    tfWriteOffDetails.setOrderCode(tfReceiptOrder.getOrderCode());
                    tfWriteOffDetails.setVoucherCode(hmdEntry.getValue().get(0).getDetails_vouchercode());
                    tfWriteOffDetails.setVoucherTypeCode(hmdEntry.getValue().get(0).getDetails_vouchertype_code());
                    tfWriteOffDetails.setVoucherTypeName(hmdEntry.getValue().get(0).getDetails_vouchertype_name());
                    tfWriteOffDetails.setOrigallowancesAmount(hmdEntry.getValue().get(0).getDetails_origallowancesamount());
                    tfWriteOffDetails.setOrigcurrentAmount(hmdEntry.getValue().get(0).getDetails_origcurrentamount());
                    writeOffDetails.add(tfWriteOffDetails);
                }
        }
        saveOrUpdateBatch(tfReceiptOrders);
//        tfArapMultiSettleDetailsService.saveOrUpdateBatch(arapMultiSettleDetails);
        tfWriteOffDetailsService.saveOrUpdateBatch(writeOffDetails);

    }
}
